package com.blustreaksw.cyclecoach.settings;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.blustreaksw.cyclecoach.btle.BleDefinedUUIDs;
import com.blustreaksw.cyclecoach.btle.ScanningActivity;
import com.blustreaksw.cyclecoach.util.ItemListMap;
import com.example.homescreendemosidemenu.R;

public class DeviceSettingsFragment extends Fragment {
	
	/*
	 * SharedPreferences string definitions
	 */
	private static final String SP_BTLE_DEVICE_PAIRING = "com.blustreaksw.cyclecoach.sp_btle_device_pairing";
	private static final String SP_BTLE_DEVICE_TYPE_UUID = "com.blustreaksw.cyclecoach.sp_btle_device_type_uuid";
	private static final String SP_BTLE_DEVICE_ADDRESS = "com.blustreaksw.cyclecoach.sp_btle_device_address";
	private static final String SP_BTLE_DEVICE_NAME = "com.blustreaksw.cyclecoach.sp_btle_device_name";
	private static final String SP_BTLE_USER_NAME = "com.blustreaksw.cyclecoach.sp_btle_user_name";
	private static final String SP_BTLE_USER_PRESSED_CONNECT = "com.blustreaksw.cyclecoach.sp_btle_user_pressed_connect";
	
	/*
	 * Layout: fragment_settings_devices.xml
	 */
	private TextView mDeviceName_heartRateMonitor;
	private Button mDeviceSettings_heartRateMonitor_pairingButton;
	private Button mDeviceSettings_heartRateMonitor_testButton;
	private TextView mDeviceAddress_heartRateMonitor;
	
	private TextView mDeviceName_speedCadence;
	private Button mDeviceSettings_speedCadence_pairingButton;
	private Button mDeviceSettings_speedCadence_testButton;
	private TextView mDeviceAddress_speedCadence;
	
	private TextView mDeviceName_cyclePowerMeter;
	private Button mDeviceSettings_cyclePowerMeter_pairingButton;
	private Button mDeviceSettings_cyclePowerMeter_testButton;
	private TextView mDeviceAddress_cyclePowerMeter;
	
	private static final String mUserName = "tomas";
	
	
	
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";

	/**
	 * The content this fragment is presenting.
	 */
	private ItemListMap.ItemLabel mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public DeviceSettingsFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = ItemListMap.ITEM_MAP.get(getArguments().getString(
					ARG_ITEM_ID));
		}
		
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_settings_devices,
				container, false);
		configureLayoutElements(rootView);
		connectLayoutElements(rootView);

		return rootView;
	}
	
	
	private void configureLayoutElements(View v) {
		mDeviceName_heartRateMonitor = (TextView) v.findViewById(R.id.deviceName_heartRateMonitor);
		mDeviceSettings_heartRateMonitor_pairingButton = (Button) v.findViewById(R.id.deviceSettings_heartRateMonitor_pairingButton);
		mDeviceSettings_heartRateMonitor_testButton = (Button) v.findViewById(R.id.deviceSettings_heartRateMonitor_testButton);
		mDeviceAddress_heartRateMonitor = (TextView) v.findViewById(R.id.deviceAddress_heartRateMonitor);
		
		mDeviceName_speedCadence = (TextView) v.findViewById(R.id.deviceName_speedCadence);
		mDeviceSettings_speedCadence_pairingButton = (Button) v.findViewById(R.id.deviceSettings_speedCadence_pairingButton);
		mDeviceSettings_speedCadence_testButton = (Button) v.findViewById(R.id.deviceSettings_speedCadence_testButton);
		mDeviceAddress_speedCadence = (TextView) v.findViewById(R.id.deviceAddress_speedCadence);
		
		mDeviceName_cyclePowerMeter = (TextView) v.findViewById(R.id.deviceName_cyclePowerMeter);
		mDeviceSettings_cyclePowerMeter_pairingButton = (Button) v.findViewById(R.id.deviceSettings_cyclePowerMeter_pairingButton);
		mDeviceSettings_cyclePowerMeter_testButton = (Button) v.findViewById(R.id.deviceSettings_cyclePowerMeter_testButton);
		mDeviceAddress_cyclePowerMeter = (TextView) v.findViewById(R.id.deviceAddress_cyclePowerMeter);
	}
	
	private void connectLayoutElements(View v) {
		mDeviceSettings_heartRateMonitor_pairingButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(), ScanningActivity.class);
				i.putExtra(ScanningActivity.USER_NAME, mUserName);
				i.putExtra(ScanningActivity.BTLE_EXTRA_DEVICE_SERVICE, BleDefinedUUIDs.Service.HEART_RATE.toString());
				startActivity(i);
			}
		});
		
		mDeviceSettings_speedCadence_pairingButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("DeviceSettingsActivity", "Searching for Cycling Speed and Cadence sensor.");
				Intent i = new Intent(getActivity(), ScanningActivity.class);
				i.putExtra(ScanningActivity.USER_NAME, mUserName);
				i.putExtra(ScanningActivity.BTLE_EXTRA_DEVICE_SERVICE, BleDefinedUUIDs.Service.CYCLING_SPEED_CADENCE.toString());
				startActivity(i);
			}
		});

		mDeviceSettings_cyclePowerMeter_pairingButton.setOnClickListener(new View.OnClickListener() {
	
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(), ScanningActivity.class);
				i.putExtra(ScanningActivity.USER_NAME, mUserName);
				i.putExtra(ScanningActivity.BTLE_EXTRA_DEVICE_SERVICE, BleDefinedUUIDs.Service.CYCLING_POWER.toString());
				startActivity(i);
			}
		});
	}
}
